package com.daisuxia.web.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.daisuxia.back.utils.SysCacheUtils;
import com.daisuxia.risk.pojo.RiskCreditUser;
import com.daisuxia.risk.service.IRiskCreditUserService;
import com.daisuxia.risk.utils.RiskSpider;
import com.daisuxia.web.common.reslult.Status;
import com.daisuxia.web.pojo.BackConfigParams;
import com.daisuxia.web.pojo.User;
import com.daisuxia.web.service.IUserService;
import com.daisuxia.web.util.JSONUtil;

/**
 * 用户支付宝，淘宝认证
 * 
 * @name
 * @date 2016年12月19日 11:10:24
 */
@Controller
public class UserAlipayController extends BaseController {
	Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private IRiskCreditUserService riskCreditUserService;
	@Autowired
	private IUserService userService;

	/**
	 * 查询支付宝用户信息
	 * 
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping("credit-alipay/get-user-info")
	public void alipayUserInfo(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Map<String, Object> json = new HashMap<String, Object>();
		String code = Status.FAILD.getName();
		String msg = "获取失败";
		Map<String, Object> data = new HashMap<String, Object>();
		try {
			User user = this.loginFrontUserByDeiceId(request);
			user = userService.searchByUserid(Integer.valueOf(user.getId()));
			String param = request.getParameter("data");
//			logger.info("alipayUserInfo error param=" + param);
			if (StringUtils.isNotBlank(param)) {
				JSONObject jsonObject = JSONObject.fromObject(param);
				String result = jsonObject.getString("money");
				if (result != null) {
					RiskCreditUser riskUser = new RiskCreditUser();
					riskUser.setUserId(Integer.valueOf(user.getId()));
					riskUser.setMyHb(new BigDecimal(result.trim()));
					riskCreditUserService.updateUserMyHb(riskUser);
					msg = "认证成功";
					code = Status.SUCCESS.getName();
					logger.info("alipay alipayUserInfo success");
				} else {
					msg = "花呗值为空！";
				}
			} else {
				msg = "参数非法！";
			}
			logger.error("jxl getAlipayUserInfo success ");
		} catch (Exception e) {
			logger.error("alipayUserInfo error ", e);
		} finally {
			data.put("message", msg);
			json.put("code", code);
			json.put("data", data);
			JSONUtil.toObjectJson(response, JSONUtil.beanToJson(json));
		}
	}

	/**
	 * 查询淘宝用户信息
	 * 
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping("credit-taobao/get-user-info")
	public void taobaoUserInfo(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Map<String, Object> json = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();
		String code = Status.FAILD.getName();
		String msg = "获取失败";

		HashMap<String, Object> paramMap = getParametersO(request);
		// String cookie2 = request.getParameter("data[cookie2]");
		// String cookie22 = request.getParameter("cookie2");
		String sessionId = (String) paramMap.get("SESSIONID");

		String taobaoKey = request.getParameter("taobaoKey");// 获取session
		String taobaoValue = request.getParameter("taobaoValue");

		User user = this.loginFrontUserByDeiceId(request);
		user = userService.searchByUserid(Integer.valueOf(user.getId()));
		try {
			if (taobaoKey != null && taobaoValue != null) {
				// System.out.println(cookie2);
				// System.out.println(sessionId);
				// System.out.println(userId);
				code = Status.SUCCESS.getName();
				;
				msg = "认证成功";
			}

			logger.error("jxl getTaobaoUserInfo success ");
		} catch (Exception e) {
			logger.error("jxl getTaobaoUserInfo error ", e);
			code = Status.ERROR.getName();
			msg = Status.ERROR.getValue();
		} finally {

			// result.put("code",code);
			// result.put("message",msg);
			// data.put("item", json);
			// result.put("data", data);

			data.put("message", msg);
			json.put("code", code);
			json.put("data", data);
			JSONUtil.toObjectJson(response, JSONUtil.beanToJson(json));
		}

	}

}
